//
//  HSPpriceMoneyViewController.m
//  Icar
//
//  Created by dllo on 15/10/18.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "HSPpriceMoneyViewController.h"
#import "BaseHeader.h"
#import "AFNBlock.h"
#import "HSPFirstCell.h"
#import "HSPWebViewController.h"
@interface HSPpriceMoneyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tablePriceMoneyView;
@property (nonatomic,strong)UIView *titleView;
@property (nonatomic,strong)UILabel *priceLabel;
@property (nonatomic,strong)UIButton *moneyButton;
@property (nonatomic,assign)BOOL isUp;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong)NSMutableArray *bigArray;
@property (nonatomic,strong)UITableView *tableViewRange;
@property (nonatomic,strong)NSArray *array;
@property (nonatomic,strong)NSArray *priceArray;
@property (nonatomic,strong)UIButton *darkView;
@property (nonatomic,strong)UIView *lineView;

@end

@implementation HSPpriceMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.bigArray = [[NSMutableArray alloc]init];
    [self creatable];
    [self rangetable];
    [self titleTitle];
    [self AlbumaHeader];
    [self AlbumaFooter];
    _array = [[NSArray alloc]initWithObjects:PRICE10,PRICE15,PRICE20,PRICE30,PRICE40,PRICE50, nil];
    self.priceArray = [[NSArray alloc]initWithObjects:@"≤10万",@"10-15万",@"15-20万",@"20-30万",@"30-50万",@"≥50万", nil];
    
}
- (void)titleTitle
{
    _titleView = [[UIView alloc] initWithFrame:CGRectMake(-15, -20, SCREEN_WIDTH, 64)];
    [_titleView setBackgroundColor:BLUECOLOR];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(25, 22, 60, 40)];
    [backButton addTarget:self action:@selector(backBack) forControlEvents:UIControlEventTouchUpInside];
    [_titleView addSubview:backButton];
    [self.navigationController.navigationBar addSubview:_titleView];

    _priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 30, 22, 100, 40)];
    _priceLabel.font =[UIFont systemFontOfSize:22.];
    _priceLabel.text = [NSString stringWithFormat:@"%@万",self.titleString];
    _priceLabel.textColor = [UIColor whiteColor];
    _priceLabel.textAlignment = NSTextAlignmentCenter;
    [_titleView addSubview:_priceLabel];
    
    _moneyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _moneyButton.frame = CGRectMake(300, 22, 100, 40);
    
    [_moneyButton setTitle:@"价格范围" forState:UIControlStateNormal];
    [_moneyButton addTarget:self action:@selector(changeCont:) forControlEvents:UIControlEventTouchUpInside];
    [_titleView addSubview:_moneyButton];
}
- (void)rangetable
{
    _tableViewRange = [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, 64, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tableViewRange.delegate = self;
    _tableViewRange.dataSource =self;
    [self.view addSubview:_tableViewRange];
    
    UILabel *rangeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, ADJUSTHEIGHT * 200, 70)];
    [rangeLabel setText:@"价格范围"];
    [rangeLabel setTextColor:[UIColor grayColor]];
    [rangeLabel setTextAlignment:NSTextAlignmentCenter];
    [self.tableViewRange setTableHeaderView:rangeLabel];
    
  
}
-(void)setUpDartView
{
    self.darkView = [[UIButton alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH - 250, SCREEN_HEIGHT)];
    [self.darkView setBackgroundColor:[UIColor blackColor]];
    [self.darkView addTarget:self action:@selector(darkClick) forControlEvents:UIControlEventTouchUpInside];
    self.darkView.alpha = 0.5;
    [self.view addSubview:self.darkView];
}
-(void)darkClick
{
    [UIView animateWithDuration:0.5 animations:^{
        _tableViewRange.frame = CGRectMake(SCREEN_WIDTH, 64, SCREEN_WIDTH, SCREEN_HEIGHT);
        [self.view bringSubviewToFront:_tableViewRange];
        self.darkView.hidden = YES;
    }];
}
#pragma 上拉刷新
- (void)AlbumaHeader
{
    __block HSPpriceMoneyViewController *vc = self;
    [_tablePriceMoneyView addLegendHeaderWithRefreshingBlock:^{
        vc.page = 1;
        [vc APPadndatanumber :1];
        _isUp = YES;
    }];
    [_tablePriceMoneyView.header beginRefreshing];
}
#pragma 下拉加载
- (void)AlbumaFooter
{
    __block HSPpriceMoneyViewController *vcc = self;
    [_tablePriceMoneyView addLegendFooterWithRefreshingBlock:^{
        vcc.page = vcc.page + 1;
        [vcc APPadndatanumber:vcc.page];
        _isUp = NO;
    }];
}
- (void)APPadndatanumber:(NSInteger)number
{
    if ([Reachability isConnectedInternet] == YES) {
        [AFNBlock getDataWithString:[NSString stringWithFormat:self.priceUrl,_page] block:^(id back) {
            if (_isUp == YES) {
                [self.bigArray removeAllObjects];
            }
            for (NSDictionary *dic in [back objectForKey:@"data"]) {
                HSPShopperModel *model = [HSPShopperModel modelWithDictionary:dic];
                [self.bigArray addObject:model];
            }
            [self.tablePriceMoneyView reloadData];
            [self.tablePriceMoneyView.header endRefreshing];
            [self.tablePriceMoneyView.footer endRefreshing];
        }];
    }
}
- (void)changeCont:(UIButton *)sender
{
     sender.selected = !sender.selected;
    if (sender.selected == YES) {
        [UIView animateWithDuration:0.5 animations:^{
            _tableViewRange.frame = CGRectMake(SCREEN_WIDTH, 64, SCREEN_WIDTH, SCREEN_HEIGHT);
            [self.view bringSubviewToFront:_tableViewRange];
            self.darkView.hidden = YES;
            _lineView.hidden = YES;
        }];
    }
    //弹出位置
    else
    {
        [self setUpDartView];
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(125, 0, SCREEN_WIDTH - 120, ADJUSTHEIGHT * 620)];
        _lineView.backgroundColor = [UIColor whiteColor];
        [UIView animateWithDuration:0.5 animations:^{
            _tableViewRange.frame = CGRectMake(0, ADJUSTHEIGHT * 64, SCREEN_WIDTH - 120, ADJUSTHEIGHT * 400);
            self.darkView.hidden = NO;
            [_lineView addSubview:_tableViewRange];
            [self.view addSubview:_lineView];
        }];
        
    }
   
}
- (void)creatable
{
    _tablePriceMoneyView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tablePriceMoneyView.delegate = self;
    _tablePriceMoneyView.dataSource =self;
    [self.view addSubview:_tablePriceMoneyView];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:_tablePriceMoneyView]) {
         return _bigArray.count;
    }
    
    return self.priceArray.count;
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:_tablePriceMoneyView]) {
        return 85;
    }
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:_tablePriceMoneyView]) {
        HSPFirstCell *cell =[tableView dequeueReusableCellWithIdentifier:@"money"];
        if (cell == nil) {
            cell = [[HSPFirstCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"money"];
        }
        HSPShopperModel *data = [self.bigArray objectAtIndex:indexPath.row];
        self.tablePriceMoneyView.scrollsToTop = YES;
        [cell getShopImage:data];
        return cell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"price"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"price"];
    }
//    tableView.separatorStyle = NO;
    cell.textLabel.text = [self.priceArray objectAtIndex:indexPath.row];
    return cell;
  
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([tableView isEqual:_tablePriceMoneyView]) {
        HSPWebViewController *push = [[HSPWebViewController alloc]init];
        [self.navigationController pushViewController:push animated:YES];
    }
    else if ([tableView isEqual: _tableViewRange])
    {
        self.priceLabel.text = self.priceArray[indexPath.row];
        self.priceUrl = self.array[indexPath.row];
        [self AlbumaHeader];
        [self AlbumaFooter];
        _lineView.hidden = YES;
        _darkView.hidden = YES;
    }
}
- (void)backBack
{
    [self.navigationController popViewControllerAnimated:YES];
    _titleView.hidden = YES;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
